package com.cyl.manager.oms.controller;

import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.enums.BusinessType;
import com.cyl.manager.oms.convert.OrderLockConvert;
import com.cyl.manager.oms.domain.entity.OrderLock;
import com.cyl.manager.oms.domain.query.OrderLockQuery;
import com.cyl.manager.oms.domain.vo.OrderLockVO;
import com.cyl.manager.oms.service.OrderLockService;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
/**
 * 锁单管理Controller
 *
 * @author zcc
 * @date 2025-02-11
 */
@Api(description ="锁单管理接口列表")
@RestController
@RequestMapping("/oms/orderLock")
public class OrderLockController extends BaseController {
    @Autowired
    private OrderLockService service;
    @Autowired
    private OrderLockConvert convert;

    @ApiOperation("查询锁单管理列表")
    @PreAuthorize("@ss.hasPermi('oms:orderLock:list')")
    @PostMapping("/list")
    public ResponseEntity<Page<OrderLock>> list(@RequestBody OrderLockQuery query, Pageable page) {
    	Long qrDeptZbjId = SecurityUtils.getDeptZbjId();
        if(qrDeptZbjId!=null) {
        	query.setDeptZbjId(qrDeptZbjId);
        }
        List<OrderLock> list = service.selectList(query, page);
        return ResponseEntity.ok(new PageImpl<>(list, page, ((com.github.pagehelper.Page)list).getTotal()));
    }

    @ApiOperation("导出锁单管理列表")
    @PreAuthorize("@ss.hasPermi('oms:orderLock:export')")
    @Log(title = "锁单管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public ResponseEntity<String> export(OrderLockQuery query) {
    	query.setDeptZbjId(SecurityUtils.getDeptZbjId());
        List<OrderLock> list = service.selectList(query, null);
        ExcelUtil<OrderLockVO> util = new ExcelUtil<>(OrderLockVO.class);
        return ResponseEntity.ok(util.writeExcel(convert.dos2vos(list), "锁单管理数据"));
    }

    @ApiOperation("获取锁单管理详细信息")
    @PreAuthorize("@ss.hasPermi('oms:orderLock:query')")
    @GetMapping(value = "/{id}")
    public ResponseEntity<OrderLock> getInfo(@PathVariable("id") Long id) {
        return ResponseEntity.ok(service.selectById(id));
    }

    @ApiOperation("新增锁单管理")
    @PreAuthorize("@ss.hasPermi('oms:orderLock:add')")
    @Log(title = "锁单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public ResponseEntity<Integer> add(@RequestBody OrderLock orderLock) {
    	 Long qrDeptZbjId = SecurityUtils.getDeptZbjId();
         if(qrDeptZbjId==null) {
             throw new RuntimeException("直播间账号才能操作此功能");
         }
         orderLock.setDeptZbjId(qrDeptZbjId);
         return ResponseEntity.ok(service.insert(orderLock));
    }

    @ApiOperation("修改锁单管理")
    @PreAuthorize("@ss.hasPermi('oms:orderLock:edit')")
    @Log(title = "锁单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResponseEntity<Integer> edit(@RequestBody OrderLock orderLock) {
        return ResponseEntity.ok(service.update(orderLock));
    }

//    @ApiOperation("删除锁单管理")
//    @PreAuthorize("@ss.hasPermi('oms:orderLock:remove')")
//    @Log(title = "锁单管理", businessType = BusinessType.DELETE)
//	@DeleteMapping("/{ids}")
//    public ResponseEntity<Integer> remove(@PathVariable Long[] ids) {
//    	return ResponseEntity.ok(service.startOrderLock(ids[0]));
//        //return ResponseEntity.ok(service.deleteByIds(ids));
//    }
    
    
    @ApiOperation("开始锁单")
    @PreAuthorize("@ss.hasPermi('oms:orderLock:add')")
    @Log(title = "开始锁单", businessType = BusinessType.INSERT)
    @PostMapping("/startOrderLock")
    public ResponseEntity<Integer> startOrderLock(@RequestBody Long id) {
    	return ResponseEntity.ok(service.startOrderLock(id));
    }
    
 
}
